<?php
	$search = $this->get('search');
	$save = $this->get('save');
	if (COUNT($_POST) > 0)
	{
		Connection::getConnect();
		if ($search)
		{
    		$key = $this->get('key');
    		if (strlen($key) == 0)
    			exit;
			if (is_numeric($key))
			{
				Connection::setQueryf("SELECT iid, name FROM item_details id JOIN item_inventory ii USING(iid) WHERE id.iid=%d OR ii.barcode=%d ORDER BY name", $key, $key);
			} else {
    			Connection::setQuery("SELECT iid, name FROM item_details WHERE name LIKE '%$key%' ORDER BY name");
			}
    		$list = Connection::fetchAll(MYSQL_ASSOC);
    		foreach ($list as $item)
    		{
    			echo '<tr class="list ease ui-text-center" iid="' . $item['iid'] . '">';
    			echo '	<td style="width: 310px;">' . $item['name'] . '</td>';
    			echo '</tr>';
    		}
    		exit;
		}
		
		if ($save)
		{
    		$id = $this->get('id');
			$returnee = $this->get('name'); 
			$reason = $this->get('reason'); 
			$qty = $this->get('qty'); 
    		Connection::setQueryf("INSERT INTO item_return(iid, reason, returnee, qty, aid, date) VALUES(%d, '%s', '%s', %d, %d, NOW());", $id, $reason, $returnee, $qty, $login->aid);
    		exit;
		}
	}
?>